package com.aaa.system.service.imp;

import com.aaa.entity.Menu;
import com.aaa.entity.User;
import com.aaa.system.mapper.MenuMapper;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.MenuService;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImp implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户的信息根据用户的id
     * @param userid
     * @return
     */
    @Override
    public List<Menu> findByUserid(Long userid) {
        return menuMapper.selectByUserid(userid);
    }

    /**
     * 根据用户的查询用户的菜单
     * @return
     */
    @Override
    public Result<List<Menu>> getLeftMenu() {
        //获取当前用户的手机号
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = TokenUtils.getInfo(token);
        String phone = (String) map.get("username");
        //该用户所具有的菜单
        List<Menu> menuList = menuMapper.findLeftMenu(phone);

        //1、查询所有的一级菜单
        List<Menu> first = menuList.stream().filter(item -> item.getParentId() == 0).collect(Collectors.toList());
        //2、查询一级菜单对应的二级菜单
        for (Menu f : first) {
            f.setChildren(getChildren(f.getMenuId(),menuList));
        }
        return new Result<>(200,"查询菜单成功",first);
    }

    @Override
    public Result<List<Menu>> getAllMenu() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        //查询所有的菜单
        List<Menu> menus = menuMapper.selectList(wrapper);
        //1、查询所有的一级菜单
        List<Menu> firsts = menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
        //2、查询一级菜单对应的二级菜单
        for (Menu f : firsts) {
            f.setChildren(getChildren(f.getMenuId(),menus));
        }
        return new Result<>(200,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Integer>> findByRoleId(Long roleId) {
        List<Integer> list =menuMapper.getMenuByUserId(roleId);
        return new Result<>(200,"查询角色的菜单id",list);
    }

    @Transactional
    @Override
    public Result<String> confirmMenu(Long roleId, Long[] menuIds) {
        //根据对象删除角色的菜单
        menuMapper.deleteByRoleId(roleId);
        //批量添角色的菜单
        if (menuIds.length!=0){
            menuMapper.batchInsert(roleId,menuIds);
        }
        return new Result<>(200,"分配角色的菜单成功");
    }

    @Override
    public Result<List<Menu>> getMenuList(Menu menu) {

        QueryWrapper<Menu> wrapper =new QueryWrapper<>();
        if (StringUtils.isNotBlank(menu.getMenuName())){
            wrapper.like("menu_name",menu.getMenuName());
        }
        if (Objects.nonNull(menu.getStatus())){
            wrapper.eq("status",menu.getStatus());
        }
        List<Menu> menus = menuMapper.selectList(wrapper);
        //1、查询所有的一级菜单
        List<Menu> firsts = menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
        //2、查询一级菜单对应的二级菜单
        for (Menu f : firsts) {
            f.setChildren(getChildren(f.getMenuId(),menus));
        }
        return new Result<>(200,"查询菜单成功",firsts);
    }

    @Override
    public Result<Boolean> saveOrUpdateMenu(Menu menu) {
        //获取创建人
        String token = WebUtil.getRequest().getHeader("token");
        String username = TokenUtils.getInfo(token).get("username").toString();
        //根据手机号进行查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",username);
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        User userdata = userMapper.selectOne(queryWrapper);
        //添加菜单 如果有id则为修改 否则为添加
        if (Objects.nonNull(menu.getMenuId())){
            //修改
            menu.setUpdateBy(userdata.getUserName());
            menu.setUpdateTime(new Date());
            int i = menuMapper.updateById(menu);
            return new Result<>(200,"修改成功",i>0?true:false);
        }
        //添加
        menu.setCreateTime(new Date());
        menu.setCreateBy(userdata.getUserName());
        menu.setIcon("el-icon-menu");
        int i = menuMapper.insert(menu);
        return new Result<>(200,"添加成功",i>0?true:false);
    }

    @Transactional
    @Override
    public Result<Boolean> delOneMenu(Long menuId) {
        //删除菜单 删除菜单表 删除role_menu
        // 删除菜单表
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_id",menuId);
        int i = menuMapper.delete(wrapper);
        menuMapper.delRM(menuId);
        return new Result<>(200,"删除菜单成功",i>0?true:false);
    }

    //获取多级菜单
    private List<Menu> getChildren(Long parentId,List<Menu> menuList){
        List<Menu> seconds=new ArrayList<>();
        for(Menu m:menuList){
            if(m.getParentId().equals(parentId)){
                seconds.add(m);
            }
        }
        for(Menu m:seconds){
            m.setChildren(getChildren(m.getMenuId(),menuList));
        }
        return seconds;
    }
}
